﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.IntelliTrace;
using System.Diagnostics;
using System.Collections.Specialized;
using System.Globalization;

namespace Microsoft.VisualStudio.Samples
{
    public class DiagnosticChainConsoleWriter : ChainConsoleWriter, DiagnosticChainHandler
    {

        private Writer Writer { get; set; }
        private IntelliTraceProcess Process { get; set; }

        public void DiagnosticEvent(IntelliTraceEvent ev)
        {
            ResolvedDiagnosticEvent diagnosticEvent = new ResolvedDiagnosticEvent(this.Process, ev as DiagnosticEvent);

            NameValueCollection eventProperties = new NameValueCollection();
            eventProperties.Add("Ordinal", diagnosticEvent.DiagnosticEvent.Ordinal.ToString(CultureInfo.CurrentCulture));
            eventProperties.Add("Event time", GetDateTimeFromEventTime(this.Process, diagnosticEvent.DiagnosticEvent.EventTime).ToString("HH:mm:ss.fffffff", CultureInfo.CurrentCulture));
            eventProperties.Add("Thread id", diagnosticEvent.DiagnosticEvent.NativeThreadId.ToString(CultureInfo.CurrentCulture));
            eventProperties.Add("Event id", diagnosticEvent.DiagnosticEvent.EventId.ToString(CultureInfo.CurrentCulture));
            eventProperties.Add("Name", diagnosticEvent.DiagnosticEvent.Name);
            eventProperties.Add("Category id", diagnosticEvent.CategoryId);
            eventProperties.Add("Short description", diagnosticEvent.GetShortDescription());
            eventProperties.Add("Long description", diagnosticEvent.GetLongDescription());
            IList<IDataElement> diagnosticEventData = diagnosticEvent.GetData();
            eventProperties.Add("Data", string.Format(CultureInfo.CurrentCulture, "{0} elements", diagnosticEventData.Count));

            DumpProperties(eventProperties);
            DumpDataElements(diagnosticEventData, 0);

            Writer.WriteLine(s_nameValueFormat, "Stack", string.Format(CultureInfo.CurrentCulture, "{0} frames", diagnosticEvent.DiagnosticEvent.StackFrames.Count));
            DumpStack(this.Process, diagnosticEvent.DiagnosticEvent.StackFrames);
        }

        
        public void UIRecordedEvent(IntelliTraceEvent ev)
        {
            DebuggerInteractionEvent debuggerPoint = ev as DebuggerInteractionEvent;
            Debug.Assert(debuggerPoint != null);

            NameValueCollection eventProperties = new NameValueCollection();
            eventProperties.Add("Ordinal", debuggerPoint.Ordinal.ToString(CultureInfo.CurrentCulture));
            eventProperties.Add("Thread id", debuggerPoint.NativeThreadId.ToString(CultureInfo.CurrentCulture));
            eventProperties.Add("UI location", debuggerPoint.DebuggerInteractionSource.ToString());
            eventProperties.Add("Stack frame index", debuggerPoint.StackFrameIndex.ToString(CultureInfo.CurrentCulture));
            eventProperties.Add("Data", string.Format(CultureInfo.CurrentCulture, "{0} elements", debuggerPoint.Data.Count));

            DumpProperties(eventProperties);

            DumpDataElements(debuggerPoint.Data, 0);
        }
    }
}
